# Contents: Library Class
# Purpose: To manager all songs and related data
# Status: Needs to be converted to Hash Table / Map collection

require 'Song'

class Library
	def initialize(filename)
		@my_library = Array.new
		@my_library = File_Manager.load_file(filename)
	end
  
	def add_song(a)
		@my_library<<a #stick a into the library
	end
  
	def print_library_debug
		@my_library.each do |s|
			print s.to_response
      print "\r\n"
		end
	end

	def each
		@my_library.each do |s|
			return s
		end
	end
  
	def update_song(mhash, rating, playcount, lastplayed)
		@my_library.each do |s|
			if (s.hash == mhash)
				if (rating!=0)
					s.set_rating(rating)
				end
				if (playcount != 0)
					s.increment_playcount(playcount)
				end
				if (lastplayed != 0)
					s.set_last_played(lastplayed.strftime("%Y%m%d %H:%M:%S"))
				end
  			break
			end
		end
	end
	
	def save(filename)
		print "Library / Save"
		File_Manager.save_file(@my_library, filename)
	end
end
